package com.sibu.yx.promotion.admin.web.support.config;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;

import com.sibu.yx.promotion.admin.web.support.filter.CustomLogoutFilter;
import com.sibu.yx.promotion.admin.web.support.filter.CustomTokenFilter;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import com.sibu.yx.promotion.admin.web.support.filter.CustomShiroRedisSessionDao;
import com.sibu.yx.promotion.admin.web.support.filter.CustomWebSessionManage;
import com.sibu.yx.promotion.admin.web.support.model.MyShiroRealm;

/**
 * @author ：yuwenbo
 * @date ：Created in 2019/4/11 17:45
 * @description：shiro配置中心
 */
@Configuration
public class ShiroConfig {


    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/login", "anon");
        filterChainDefinitionMap.put("/sendCode","anon");
        filterChainDefinitionMap.put("/logout", "logout");

        filterChainDefinitionMap.put("/", "anon");
        filterChainDefinitionMap.put("/**", "authc");
        shiroFilterFactoryBean.setFilters(createCustomFilerMap());
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator =  new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }


    /**
     * 创建本地的拦截器
     *
     * @return
     */
    private Map<String, Filter> createCustomFilerMap() {
        Map<String, Filter> filterMap = new HashMap<>();
        //DefaultFilter ,替换默认的拦截器
        filterMap.put(DefaultFilter.authc.name(), new CustomTokenFilter());
        filterMap.put(DefaultFilter.logout.name(), new CustomLogoutFilter());
        return filterMap;
    }


    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setSessionManager(customWebSessionManage());
        securityManager.setRealms(Arrays.asList(shiroRealm()));
        return securityManager;
    }

    /**
     * 本地缓存管理
     *
     * @return
     */
    public CustomWebSessionManage customWebSessionManage() {
        CustomWebSessionManage customWebSessionManage = new CustomWebSessionManage();
        customWebSessionManage.setSessionValidationSchedulerEnabled(false);
        customWebSessionManage.setSessionIdCookieEnabled(true);
        customWebSessionManage.setSessionDAO(new CustomShiroRedisSessionDao());
        Cookie cookie = new SimpleCookie();
//        cookie.setSameSite(Cookie.SameSiteOptions.NONE);
        cookie.setDomain(null);
        cookie.setName("web.uid");
//        cookie.setDomain("/");
        cookie.setMaxAge((int) Duration.ofHours(4).toMillis());
        customWebSessionManage.setSessionIdCookie(cookie);
        return customWebSessionManage;
    }


    public MyShiroRealm shiroRealm() {
        MyShiroRealm shiroRealm = new MyShiroRealm();
        return shiroRealm;
    }


//    public TokenShiroRealm tokenShiroRealm() {
//        TokenShiroRealm tokenShiroRealm = new TokenShiroRealm();
//        tokenShiroRealm.setPermissionResolver(new CustomPermissionResolver());
//        return tokenShiroRealm;
//    }


}
